﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HuynhQuangWebsite.Models;
using System.IO;
using THS.HtmlHelpers;
namespace HuynhQuangWebsite.Controllers
{
    public class ManagePartnerController : BaseController
    {
        huynhquangco_websiteEntities ctx = new huynhquangco_websiteEntities();
        //
        // GET: /ManagePartners/
        int PageSize = 6;
        public ActionResult IndexPartner(int page = 1)
        {
            // TODO: Add update logic here
            // if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOn", "AdminHome");
            var model = new HomeAdminListViewModel();
            IList<Customer> custs = ctx.Customers.OrderByDescending(p=>p.CustomerID).ToList();
            model.CustomerList = custs.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.CustomerInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = custs.Count() };
            return View(model);
        }
        public ActionResult Paging(int page = 1)
        {
            // TODO: Add update logic here
            // if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOn", "AdminHome");
            var model = new HomeAdminListViewModel();
            IList<Customer> custs = ctx.Customers.OrderByDescending(p => p.CustomerID).ToList();
            model.CustomerList = custs.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.CustomerInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = custs.Count() };
            return PartialView("UCPartnerList",model);
        }
        public ActionResult Detail(int id)
        {
            // TODO: Add update logic here
            // if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOn", "AdminHome");
            var partner = (from partners in ctx.Customers where partners.CustomerID == id select partners).SingleOrDefault();
            if (partner != null)
                return View(partner);
            else
            {
                partner = new Models.Customer();
                return View(partner);
            }
            //return trang thông báo lỗi
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Detail(int id, HttpPostedFileBase uploadFile)
        
        {
            try
            {
                // TODO: Add update logic here
               // if (!IsAuthenticateAdmin())
                 //   return RedirectToAction("LogOn", "AdminHome");
               Customer cust = ctx.Customers.Where(p=>p.CustomerID==id).SingleOrDefault();

                if (uploadFile != null)
                {
                    String oldImage = cust.CustomerImg;
                    String oldThumbImage = cust.CustomerImgThumb;
                    var fileName = Path.GetFileName(uploadFile.FileName);
                    String sFileName = Utilities.GenerateImageName(Request, Utilities.VirtualPath, fileName, 1.ToString());
                    string filePath = Path.Combine(Request.MapPath(Utilities.VirtualPath), sFileName);
                    String fileThumbPath = Path.Combine(Request.MapPath(Utilities.VirtualPath + "Thumbs/"), sFileName);
                   //old method
                    //MakeImagesAndThumbFile(uploadFile.InputStream, filePath, fileThumbPath, 400, 300, 130, 130, 75);
                    //save thumbnail file set width and height to fix image
                    ResizeImage(uploadFile.InputStream, fileThumbPath, 99, 99, true);//thumbnails
                    ResizeImage(uploadFile.InputStream, filePath, 400, 300, true);//main image
                    cust.CustomerImg = Utilities.PhysicalPath + sFileName;
                    cust.CustomerImgThumb = String.Format("{0}Thumbs/{1}", Utilities.PhysicalPath, sFileName);
                    //delete image
                    DeleteFile(Request, Utilities.VirtualPath, oldImage);
                    DeleteFile(Request, Utilities.VirtualPath + "Thumbs", oldThumbImage);
                }
               
                UpdateModel(cust);
                ctx.SaveChanges();
                ViewData["Msg"] = "Đã cập nhật!";
                return View(cust);
                //return RedirectToAction("IndexPartner");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                // TODO: Add delete logic here
                //if (!IsAuthenticateAdmin())
                //    return RedirectToAction("LogOn", "AdminHome");
                Customer cust = ctx.Customers.Where(p=>p.CustomerID==id).SingleOrDefault();
                String oldImage = cust.CustomerImg;
                String oldThumbImage = cust.CustomerImgThumb;
                ctx.Customers.DeleteObject(cust);
                ctx.SaveChanges();
                DeleteFile(Request, Utilities.VirtualPath, oldImage);
                DeleteFile(Request, Utilities.VirtualPath + "Thumbs", oldThumbImage);
                return RedirectToAction("IndexPartner");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /FunRingManagement/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create([Bind(Exclude = "CustomerID")]Customer cust,HttpPostedFileBase uploadFile)
        {
            // TODO: Add update logic here
            // if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOn", "AdminHome");
            try
            {
                if (uploadFile != null)
                {
                    var fileName = Path.GetFileName(uploadFile.FileName);
                    String sFileName = Utilities.GenerateImageName(Request, Utilities.VirtualPath, fileName, 1.ToString());
                    string filePath = Path.Combine(Request.MapPath(Utilities.VirtualPath), sFileName);
                    String fileThumbPath = Path.Combine(Request.MapPath(Utilities.VirtualPath + "Thumbs/"), sFileName);
                   // MakeImagesAndThumbFile(uploadFile.InputStream, filePath, fileThumbPath, 400, 300, 130, 130, 75);
                    //save thumbnail file
                    ResizeImage(uploadFile.InputStream, fileThumbPath, 99, 99, true);
                    ResizeImage(uploadFile.InputStream, filePath, 400, 300, true);

                    cust.CustomerImg = Utilities.PhysicalPath + sFileName;
                    cust.CustomerImgThumb = String.Format("{0}Thumbs/{1}", Utilities.PhysicalPath, sFileName);
                }
                ctx.AddToCustomers(cust);
                ctx.SaveChanges();
                ViewData["Msg"] = "Đã tạo thành công!";
                return View();
            }
            catch
            {
                return View();

            }
        }
    }
}
